package com.hbisdt.cet.sample.config.intercepter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Description 自定义拦截器
 * @Author Jiam
 * @Date 2025-10-08 9:40
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session中获取 loginUserName
        String loginUserName = (String) request.getSession().getAttribute("loginUserName");
        System.out.println("url:" + request.getRequestURI() + ",loginUserName:" + loginUserName);
        // 检查 loginUserName 是否存在
        if (loginUserName == null || loginUserName.isEmpty()) {
            // 如果 loginUserName 不存在，跳转登录页
            response.sendRedirect("/view/login");
            return false;
        }
        return true;
    }
}
